import 'package:eplorex/pages/community/community_page.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import '../models/bo/UnReadMessageCounter.dart';
import 'activity/activity_page.dart';
import 'message/convlist_page.dart';
import 'my/my_page.dart';
import 'package:badges/badges.dart' as badges;

class HomePage extends StatefulWidget {


  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  int _slectedIndex = 0;

  List<Widget> _pages = [
    CommunityPage(),
    ActivityPage(),
    MessagePage(),
    MyInfoPage()
  ];

  @override
  Widget build(BuildContext context) {
    final unReadCnt= Provider.of<UnReadMessageCounter>(context).unReadCnt;
    return Scaffold(
      body: _pages[_slectedIndex],
      bottomNavigationBar: BottomNavigationBar(
        showUnselectedLabels: true,
        type: BottomNavigationBarType.fixed,
        fixedColor: Colors.black,
        backgroundColor: Colors.white,
        currentIndex: _slectedIndex,
        items: <BottomNavigationBarItem>[
         const BottomNavigationBarItem(
            icon: Icon(
              Icons.home,
            ),
            label: '圈子',
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.local_activity),
            label: '发现',
          ),
          BottomNavigationBarItem(
            icon: badges.Badge(
              position: badges.BadgePosition.topEnd(top: -14, end: -5),
              showBadge: unReadCnt>0,
              badgeAnimation:
              const badges.BadgeAnimation.fade(toAnimate: false),
              badgeContent:  Text(
                "$unReadCnt",
                style: TextStyle(color: Colors.white),
              ),
              child: Icon(Icons.message),
            ),
            label: '消息',
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.person),
            label: '我',
          )
        ],
        onTap: _onItemTapped,
      ),
    );
  }

  void _onItemTapped(int idx) {
    setState(() {
      _slectedIndex = idx;
    });
  }
}
